package com.hopu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hopu.pojo.Emp;
import com.hopu.pojo.Zhichu;
import com.hopu.service.ZhichuService;
import com.hopu.util.ResultUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RequestMapping("/zhichu")
@RestController
public class ZhichuController {
    @Resource
    private ZhichuService zhichuServiceImpl;

    @PostMapping("/findAllZhichu")
    private ResultUtils findAllZhichu(@RequestParam(value = "current",defaultValue = "1") Integer current){

        try {
            PageHelper.startPage(current,6);
            List<Zhichu> allZhichu = zhichuServiceImpl.findAllZhichu();

            PageInfo pageInfo = new PageInfo(allZhichu);
            return new ResultUtils(200,"查询页面成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultUtils(9999,"查询页面失败",null);
        }
    }

    @PostMapping("addZhichu")
    public ResultUtils addZhichu(
            HttpServletRequest request, HttpServletResponse response,
            @RequestParam(value = "zmoney")Float zmoney,
            @RequestParam(value = "ztm")String ztm,
            @RequestParam(value = "zremark")String zremark

    ){
        try {
            Emp empLog = (Emp) request.getSession().getAttribute("empLog");

            int num = zhichuServiceImpl.addZhichu(empLog.getEid(),zmoney,ztm,new Date().getTime(),zremark);

            if (num==0){
                return new ResultUtils(400,"ERROR",null);
            }
            return new ResultUtils(200,"SUCCESS",1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @PostMapping("/findZhichuByZid")
    public ResultUtils findZhichuByZid(int zid){

        Zhichu zhichu = zhichuServiceImpl.findZhichuByZid(zid);

        return new ResultUtils(200,"查询页面成功",zhichu);

    }
    @PostMapping("uppZhichu")
    public ResultUtils uppZhichu(Zhichu zhichu, HttpSession session){

            // 获取session 当前登录账号信息
            Emp empLog = (Emp)session.getAttribute("empLog");
            zhichu.setEid(empLog.getEid());
            long time = new Date().getTime();
            zhichu.setZtime(time);
            int biao1 = zhichuServiceImpl.uppZhichu(zhichu);
            if (biao1<0){
                return new ResultUtils(400,"ERROR",null);
            }
            return new ResultUtils(200,"SUCCESS",1);


    }
}
